//
//  WKThreeEliteViewController.m
//  WKDemo
//
//  Created by wangzhaohui-Mac on 14-8-2.
//  Copyright (c) 2014年 com.app. All rights reserved.
//

#import "WKThreeEliteViewController.h"
#import "WKVoiceViewCell.h"
#import "WKVoiceHttpTool.h"
#import "WKVoiceParam.h"
#import "WKVoiceResult.h"

#import "WKVoiceModel.h"
#import "WKVoiceCellFrame.h"


@interface WKThreeEliteViewController ()
@property (nonatomic,strong) NSArray *statuses;
@end

@implementation WKThreeEliteViewController

#pragma mark - 懒加载
- (NSArray *)statuses
{
    if (_statuses == nil) {
        _statuses = [NSArray array];
    }
    return _statuses;
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self loadNewData];
}
/**
 *  加载新数据
 */
- (void)loadNewData
{
    WKVoiceParam *param = [[WKVoiceParam alloc] init];
    
    [WKVoiceHttpTool getEliteNewDataListWithParam:param success:^(WKVoiceResult *result) {
        //将返回的数据集合包装成frame模型
        NSMutableArray *frameArray=[NSMutableArray array];
        for (WKVoiceModel *voiceModel in result.homeDatas) {
            WKVoiceCellFrame *voiceCellFrame=[[WKVoiceCellFrame alloc] init];
            voiceCellFrame.model=voiceModel;
            [frameArray addObject:voiceCellFrame];
        }
        self.statuses = frameArray;
        
        //显示底部刷新控件
        self.tableView.footerHidden = NO;
        //刷新数据
        [self.tableView reloadData];

        //停止下拉刷新
        [self.tableView headerEndRefreshing];
        
    } failure:^(NSError *error) {
        //停止下拉刷新
        [self.tableView headerEndRefreshing];
        WKLog(@"%@",error);
    }];
    
}


#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.statuses.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    WKVoiceViewCell *cell=[WKVoiceViewCell voiceCellWithTableView:tableView];
    
    WKVoiceCellFrame *cellFrame=self.statuses[indexPath.row];
    cell.cellFrame=cellFrame;
    return cell;
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    WKVoiceCellFrame *currentFrame=self.statuses[indexPath.row];
    return currentFrame.cellHeight;
}

@end
